<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-11-16
 * Time: 14:56:28
 * Info: 幻灯片控制器
 */
declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use app\common\model\Slide as SlideModel;
use app\common\model\SlideCate as SlideCateModel;

class Slide extends Base
{

    /**
     * 列表
     */
    public function index(): string
    {
        if ($this->request->isAjax()) {
            $page  = (int)$this->request->param('page', 1);
            $limit = (int)$this->request->param('limit', 10);
            $data  = SlideModel::getList($page, $limit);
            $count = SlideModel::getCount();
            $this->success('ok', $data, $count);
        }

        return View::fetch();
    }

    /**
     * 添加
     */
    public function add(): string
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['slide_id'])) {
                $this->error('请先选择分组');
            }
            if (empty($param['img'])) {
                $this->error('图片不能为空');
            }
            try {
                SlideModel::create($param);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('操作成功');
        }
        $linkGroupData = SlideCateModel::getList();
        $linkGroupName = [];
        if ( ! empty($linkGroupData)) {
            $linkGroupName = array_column($linkGroupData, 'title', 'id');
        }
        View::assign('link_group_name', $linkGroupName);

        return View::fetch();
    }

    /**
     * 编辑
     */
    public function edit(): string
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['id'])) {
                $this->error('参数错误');
            }
            if (empty($param['slide_id'])) {
                $this->error('请先选择分组');
            }
            if (empty($param['img'])) {
                $this->error('图片地址不能为空');
            }
            try {
                SlideModel::update($param, ['id' => $param['id']]);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('操作成功');
        }
        $id = $this->request->get('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $data = SlideModel::find($id);
        if (empty($data)) {
            $this->error('获取数据失败');
        }
        $linkGroupData = SlideCateModel::getList();
        $linkGroupName = [];
        if ( ! empty($linkGroupData)) {
            $linkGroupName = array_column($linkGroupData, 'title', 'id');
        }
        View::assign('data', $data);
        View::assign('link_group_name', $linkGroupName);

        return View::fetch();
    }

    /**
     * 删除
     */
    public function delete(): string
    {
        if ($this->request->isAjax()) {
            $id = (int)$this->request->param('id');
            if (empty($id)) {
                $this->error("参数错误");
            }
            SlideModel::destroy($id);
            $this->success("删除成功");
        }
        $this->error("请求错误");
    }

}
